# 加油！杨思钦！
# 沉下心来慢慢琢磨，你是最棒的！
import uuid
from config import Config
import requests
import base64
import json


def get_img():
    uid = str(uuid.uuid4())
    url = Config.HOST + "/captcha.jpg?uuid={uid}"
    response = requests.request(method="get", url=url)
    # content就是二进制的图片文件
    content = response.content
    return content, uid


def base64_api(uname, pwd, img, typeid):
    base64_data = base64.b64encode(img)
    b64 = base64_data.decode()
    data = {"username": uname, "password": pwd, "typeid": typeid, "image": b64}
    result = json.loads(requests.post("http://api.ttshitu.com/predict", json=data).text)
    if result['success']:
        return result["data"]["result"]
    else:
        return result["message"]
    return ""


def admin_login(uname, password, code="lemon"):
    url = Config.HOST + '/adminLogin'
    method = 'post'
    data = {"principal": uname, "credentials": password, "imageCode": code}
    response = requests.request(method=method, url=url, json=data)
    try:
        content = response.json()
        token = content["access_token"]
        return token
    except Exception as e:
        raise e


def load_img(file, token):
    url = Config.HOST + '/admin/file/upload/img'
    headers = {"authorization": f"bearer{token}"}
    method = "post"
    files = {"file": open(file, mode="rb")}
    response = requests.request(url=url, method=method, files=files, headers=headers)
    return response.text


def user_login():
    url = Config.HOST + "/login"
    method = "post"
    data = {"principal": "sqyang", "credentials": "qaz147258", "loginType": 0, "appType": 3}
    response = requests.request(url=url, method=method, json=data)
    print(response.json())

if __name__ == '__main__':
   user_login()
